
<template>
    <div class="execution">
        <basic-container>
            <avue-crud
                       :permission="permissionList"
                       v-bind="bindVal"
                       v-on="onEvent"
                       :page.sync="page"
            >
              <template slot="menuLeft">
                <el-button
                  type="primary"
                  size="small"
                  v-if="permissionList.addBatchBtn"
                  @click="showAdd">添 加
                </el-button>
                <el-button
                  type="primary"
                  size="small"
                  v-if="permissionList.delBtn"
                  @click="handleBatchDel">删 除
                </el-button>
                <el-button
                  type="primary"
                  size="small"
                  @click="handleExport">导 出
                </el-button>
              </template>
            </avue-crud>
        </basic-container>
        <open-dialog title="添 加" :DialogVisible.sync="addVisible">
          <dynamic-crud ref="dynamicCrud" title="车票回收详情" :data="addList" :option="addOption">
          </dynamic-crud>
          <span slot="footer">
            <el-button type="primary" size="small" icon="el-icon-circle-plus-outline" @click="handleAdd">保存</el-button>
            <el-button size="small" icon="el-icon-circle-close" @click="hideAdd">取消</el-button>
          </span>
        </open-dialog>
    </div>
</template>

<script>
    import { addBatchObj, delBatch,exportExcel} from '@/api/subwaystm/swstrecovery'
    import {tableOption,tableOptionAdd,startDate, endDate} from '@/const/crud/subwaystm/swstrecovery'
    import {mapGetters} from 'vuex'
    import baseCrud from '@/mixins/base-crud'
    import { handleExcelResponse } from "@/util/util";

    export default {
        name: 'swstrecovery',
      mixins: [baseCrud('subwaystm/swstrecovery')],
        data() {
            return {
                addVisible: false,
                addList: [],
                userProps: {
                  label: "nikename",
                  value: "userCode",
                },
                searchForm: {
                  dates1: [startDate, endDate]
                }
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.stm_sw_st_recovery_add, false),
                    addBatchBtn: this.vaildData(this.permissions.stm_sw_st_recovery_add_batch, false),
                    delBtn: this.vaildData(this.permissions.stm_sw_st_recovery_del, false),
                    editBtn: this.vaildData(this.permissions.stm_sw_st_recovery_edit, false)
                };
            },
            addOption(){
              let column = this.deepClone(tableOptionAdd.column)
              column.forEach(c=>{
                c.search = false
                c.slot = c.formslot
              })
              return {
                column: column
              }
            }
        },
        methods: {
            async handleExport(){
              let loading = this.$loading({
                lock: true,
                text: "Loading",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)"
              })
              try {
                let tableSelect = this.$refs.crud.tableSelect;
                let ids = tableSelect.map(row => row.id)
                let param = {
                  ids: this.validatenull(ids)?null:ids
                };
                const res = await exportExcel(param);
                handleExcelResponse(res);
              } finally {
                loading.close();
              }
            },
            showAdd(){
              this.addList = [{$cellEdit: true}]
              this.addVisible = true
            },
            hideAdd(){
              this.addVisible = false
            },
            handleAdd(){
              if(this.addList.length == 0){
                this.$message.info('请添加记录')
                return
              }
              var asyncValidators = []
              var formCellRules = this.$refs.dynamicCrud.$refs.crud.formCellRules
              for(let row of this.addList){
                let rowValidator = this.asyncValidator(formCellRules, row).catch(errors => {
                  return Promise.reject({index: row.$index, errors})
                })
                asyncValidators.push(rowValidator)
              }
              Promise.all(asyncValidators).then(()=>{
                addBatchObj(this.addList).then(response => {
                  this.hideAdd()
                  this.getList(this.page)
                })
              }).catch(({index, errors}) => {
                this.$message.error(`第${index+1}行:${errors[0].message}`)
              })
            },
            handleBatchDel(){
              let tableSelect = this.$refs.crud.tableSelect
              if(tableSelect.length == 0){
                this.$message.info('请选择要删除的记录')
                return
              }
              let ids = tableSelect.map(row => row.id)
              this.$confirm(`是否确认删除${ids.length}条记录`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                return delBatch(ids)
              }).then(response => {
                this.$message.success('删除成功')
                this.getList(this.page)
              })
            }
        }
    }
</script>
